library(tidyverse)
library(plotly)
library(p8105.datasets)

Load data

data(instacart)

Filter data

instacart = 
  instacart |> 
  select (department, aisle, order_hour_of_day, order_number, days_since_prior_order)

Create Bar Chart

instacart |> 
  count(department) |> 
  filter(department != "missing") |> 
  mutate(department = fct_reorder(department, n)) |> 
  plot_ly (x = ~department, y = ~n, color = ~department,
         type = "bar", colors = "viridis") |> 
  layout(
    xaxis = list(title = "Department"),   
    yaxis = list(title = "Number of Items") 
  )

Create Box Plot

instacart |>
  filter(department != "missing") |> 
  mutate(department = fct_reorder(department, order_hour_of_day)) |>
  plot_ly(x = ~department, y = ~order_hour_of_day, color = ~department,
          type = "box", colors = "viridis") |> 
  layout(
    xaxis = list(title = "Department"),   
    yaxis = list(title = "Order Hour of Day") 
  )

Create Scatter Plot

instacart |>
  group_by(order_number) |>
  summarise(avg_days_since_prior_order = mean(days_since_prior_order, na.rm = TRUE)) |> 
  plot_ly(x = ~order_number, y = ~avg_days_since_prior_order, color = ~order_number,
      type = "scatter", mode = "markers", colors = "viridis") |> 
  layout(
    xaxis = list(title = "Order Number"),   
    yaxis = list(title = "Average Days Since Prior Order") 
  )